/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using EXtremecode.Utils;
using System.Text;
using System.Xml;
using EXtremecode.Common;
namespace CodeGenerator
{
	/// <summary>
	/// Summary description for RepositoryFrm.
	/// </summary>
	public class RepositoryFrm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button btnTemplateDir;
		private System.Windows.Forms.Button btnOutputDir;
		private System.Windows.Forms.FolderBrowserDialog fldrDialogTemplate;
		private System.Windows.Forms.FolderBrowserDialog fldrDialogOutput;
		private System.Windows.Forms.TextBox txtTemplateDir;
		private System.Windows.Forms.TextBox txtOutputDir;
        private System.Windows.Forms.ColumnHeader chEntity;
		private System.Windows.Forms.Button btnGenerate;
		private System.Windows.Forms.ColumnHeader chTableName;
		private System.Windows.Forms.ColumnHeader chViewName;
		private System.Windows.Forms.ListView lvEntities;
        private System.Windows.Forms.CheckBox chkOverwrite;
        private Button btnLoad;
        private Button btnSelectAll;
        private Button btnUnselect;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public RepositoryFrm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		[STAThread]
		static void Main(string[] args)
		{
			Console.WriteLine(System.AppDomain.CurrentDomain.FriendlyName);
			Application.Run(new RepositoryFrm());
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(RepositoryFrm));
            this.txtTemplateDir = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.txtOutputDir = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.fldrDialogTemplate = new System.Windows.Forms.FolderBrowserDialog();
            this.fldrDialogOutput = new System.Windows.Forms.FolderBrowserDialog();
            this.lvEntities = new System.Windows.Forms.ListView();
            this.chEntity = new System.Windows.Forms.ColumnHeader();
            this.chTableName = new System.Windows.Forms.ColumnHeader();
            this.chViewName = new System.Windows.Forms.ColumnHeader();
            this.chkOverwrite = new System.Windows.Forms.CheckBox();
            this.btnOutputDir = new System.Windows.Forms.Button();
            this.btnTemplateDir = new System.Windows.Forms.Button();
            this.btnLoad = new System.Windows.Forms.Button();
            this.btnSelectAll = new System.Windows.Forms.Button();
            this.btnGenerate = new System.Windows.Forms.Button();
            this.btnUnselect = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // txtTemplateDir
            // 
            this.txtTemplateDir.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtTemplateDir.BackColor = System.Drawing.Color.White;
            this.txtTemplateDir.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtTemplateDir.Location = new System.Drawing.Point(142, 8);
            this.txtTemplateDir.Name = "txtTemplateDir";
            this.txtTemplateDir.Size = new System.Drawing.Size(317, 20);
            this.txtTemplateDir.TabIndex = 0;
            this.txtTemplateDir.Text = "G:\\a v a n z a\\PersonalWork\\CodeGenerator\\CodeGenerator\\TemplateFiles";
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.Color.DarkOrange;
            this.label1.Location = new System.Drawing.Point(4, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(132, 23);
            this.label1.TabIndex = 1;
            this.label1.Text = "Template Repository Dir";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // txtOutputDir
            // 
            this.txtOutputDir.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtOutputDir.BackColor = System.Drawing.Color.White;
            this.txtOutputDir.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtOutputDir.Location = new System.Drawing.Point(142, 40);
            this.txtOutputDir.Name = "txtOutputDir";
            this.txtOutputDir.Size = new System.Drawing.Size(317, 20);
            this.txtOutputDir.TabIndex = 0;
            this.txtOutputDir.Text = "G:\\a v a n z a\\PersonalWork\\CodeGenerator\\TestProject\\LMSTTest";
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.ForeColor = System.Drawing.Color.DarkOrange;
            this.label2.Location = new System.Drawing.Point(4, 37);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(112, 23);
            this.label2.TabIndex = 1;
            this.label2.Text = "Output Dir";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lvEntities
            // 
            this.lvEntities.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.lvEntities.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lvEntities.CheckBoxes = true;
            this.lvEntities.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.chEntity,
            this.chTableName,
            this.chViewName});
            this.lvEntities.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lvEntities.FullRowSelect = true;
            this.lvEntities.GridLines = true;
            this.lvEntities.Location = new System.Drawing.Point(4, 112);
            this.lvEntities.Name = "lvEntities";
            this.lvEntities.Size = new System.Drawing.Size(489, 204);
            this.lvEntities.TabIndex = 5;
            this.lvEntities.UseCompatibleStateImageBehavior = false;
            this.lvEntities.View = System.Windows.Forms.View.Details;
            // 
            // chEntity
            // 
            this.chEntity.Text = "Entity";
            this.chEntity.Width = 239;
            // 
            // chTableName
            // 
            this.chTableName.Text = "Table Name";
            this.chTableName.Width = 84;
            // 
            // chViewName
            // 
            this.chViewName.Text = "View Name";
            this.chViewName.Width = 79;
            // 
            // chkOverwrite
            // 
            this.chkOverwrite.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.chkOverwrite.BackColor = System.Drawing.Color.Transparent;
            this.chkOverwrite.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
            this.chkOverwrite.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
            this.chkOverwrite.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.chkOverwrite.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.chkOverwrite.ForeColor = System.Drawing.Color.DarkOrange;
            this.chkOverwrite.Location = new System.Drawing.Point(223, 89);
            this.chkOverwrite.Name = "chkOverwrite";
            this.chkOverwrite.Size = new System.Drawing.Size(83, 17);
            this.chkOverwrite.TabIndex = 6;
            this.chkOverwrite.Text = "Overwrite";
            this.chkOverwrite.UseVisualStyleBackColor = false;
            // 
            // btnOutputDir
            // 
            this.btnOutputDir.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnOutputDir.BackColor = System.Drawing.Color.Transparent;
            this.btnOutputDir.BackgroundImage = global::CodeGenerator.Properties.Resources.add_edit;
            this.btnOutputDir.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.btnOutputDir.FlatAppearance.BorderSize = 0;
            this.btnOutputDir.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
            this.btnOutputDir.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
            this.btnOutputDir.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnOutputDir.Location = new System.Drawing.Point(460, 32);
            this.btnOutputDir.Name = "btnOutputDir";
            this.btnOutputDir.Size = new System.Drawing.Size(32, 34);
            this.btnOutputDir.TabIndex = 3;
            this.btnOutputDir.UseVisualStyleBackColor = false;
            this.btnOutputDir.Click += new System.EventHandler(this.btnOutputDir_Click);
            // 
            // btnTemplateDir
            // 
            this.btnTemplateDir.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnTemplateDir.BackColor = System.Drawing.Color.Transparent;
            this.btnTemplateDir.BackgroundImage = global::CodeGenerator.Properties.Resources.add_edit;
            this.btnTemplateDir.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.btnTemplateDir.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnTemplateDir.FlatAppearance.BorderSize = 0;
            this.btnTemplateDir.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
            this.btnTemplateDir.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
            this.btnTemplateDir.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnTemplateDir.Location = new System.Drawing.Point(464, 3);
            this.btnTemplateDir.Name = "btnTemplateDir";
            this.btnTemplateDir.Size = new System.Drawing.Size(27, 26);
            this.btnTemplateDir.TabIndex = 2;
            this.btnTemplateDir.UseVisualStyleBackColor = false;
            this.btnTemplateDir.Click += new System.EventHandler(this.btnTemplateDir_Click);
            // 
            // btnLoad
            // 
            this.btnLoad.BackColor = System.Drawing.Color.Transparent;
            this.btnLoad.BackgroundImage = global::CodeGenerator.Properties.Resources.ApplicationButton1;
            this.btnLoad.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.btnLoad.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnLoad.FlatAppearance.BorderSize = 0;
            this.btnLoad.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
            this.btnLoad.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
            this.btnLoad.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnLoad.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnLoad.ForeColor = System.Drawing.Color.White;
            this.btnLoad.Location = new System.Drawing.Point(4, 64);
            this.btnLoad.Name = "btnLoad";
            this.btnLoad.Size = new System.Drawing.Size(93, 40);
            this.btnLoad.TabIndex = 4;
            this.btnLoad.Text = "Load";
            this.btnLoad.UseVisualStyleBackColor = false;
            this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
            // 
            // btnSelectAll
            // 
            this.btnSelectAll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnSelectAll.BackColor = System.Drawing.Color.Transparent;
            this.btnSelectAll.BackgroundImage = global::CodeGenerator.Properties.Resources.ApplicationButton2;
            this.btnSelectAll.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.btnSelectAll.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnSelectAll.FlatAppearance.BorderSize = 0;
            this.btnSelectAll.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
            this.btnSelectAll.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
            this.btnSelectAll.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnSelectAll.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnSelectAll.ForeColor = System.Drawing.Color.White;
            this.btnSelectAll.Location = new System.Drawing.Point(312, 79);
            this.btnSelectAll.Name = "btnSelectAll";
            this.btnSelectAll.Size = new System.Drawing.Size(93, 40);
            this.btnSelectAll.TabIndex = 4;
            this.btnSelectAll.Text = "Select All";
            this.btnSelectAll.UseVisualStyleBackColor = false;
            this.btnSelectAll.Click += new System.EventHandler(this.btnSelectAll_Click);
            // 
            // btnGenerate
            // 
            this.btnGenerate.BackColor = System.Drawing.Color.Transparent;
            this.btnGenerate.BackgroundImage = global::CodeGenerator.Properties.Resources.ApplicationButton1;
            this.btnGenerate.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.btnGenerate.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnGenerate.FlatAppearance.BorderSize = 0;
            this.btnGenerate.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
            this.btnGenerate.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
            this.btnGenerate.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnGenerate.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnGenerate.ForeColor = System.Drawing.Color.White;
            this.btnGenerate.Location = new System.Drawing.Point(95, 64);
            this.btnGenerate.Name = "btnGenerate";
            this.btnGenerate.Size = new System.Drawing.Size(93, 40);
            this.btnGenerate.TabIndex = 4;
            this.btnGenerate.Text = "Generate";
            this.btnGenerate.UseVisualStyleBackColor = false;
            this.btnGenerate.Click += new System.EventHandler(this.btnGenerate_Click);
            // 
            // btnUnselect
            // 
            this.btnUnselect.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnUnselect.BackColor = System.Drawing.Color.Transparent;
            this.btnUnselect.BackgroundImage = global::CodeGenerator.Properties.Resources.ApplicationButton2;
            this.btnUnselect.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.btnUnselect.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnUnselect.FlatAppearance.BorderSize = 0;
            this.btnUnselect.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
            this.btnUnselect.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
            this.btnUnselect.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnUnselect.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnUnselect.ForeColor = System.Drawing.Color.White;
            this.btnUnselect.Location = new System.Drawing.Point(401, 79);
            this.btnUnselect.Name = "btnUnselect";
            this.btnUnselect.Size = new System.Drawing.Size(93, 40);
            this.btnUnselect.TabIndex = 4;
            this.btnUnselect.Text = "Uncheck All";
            this.btnUnselect.UseVisualStyleBackColor = false;
            this.btnUnselect.Click += new System.EventHandler(this.btnUnselect_Click);
            // 
            // RepositoryFrm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(497, 322);
            this.Controls.Add(this.chkOverwrite);
            this.Controls.Add(this.lvEntities);
            this.Controls.Add(this.btnOutputDir);
            this.Controls.Add(this.btnTemplateDir);
            this.Controls.Add(this.txtTemplateDir);
            this.Controls.Add(this.txtOutputDir);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.btnLoad);
            this.Controls.Add(this.btnUnselect);
            this.Controls.Add(this.btnSelectAll);
            this.Controls.Add(this.btnGenerate);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "RepositoryFrm";
            this.Text = "Generator";
            this.Load += new System.EventHandler(this.RepositoryFrm_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void btnTemplateDir_Click(object sender, System.EventArgs e)
		{
			if(txtTemplateDir.Text.Trim() != string.Empty)
			{
				fldrDialogTemplate.SelectedPath=txtTemplateDir.Text;
			}
			else
			{
				fldrDialogTemplate.SelectedPath=Directory.GetCurrentDirectory();
			}

			//open dialog.
			if(fldrDialogTemplate.ShowDialog()== DialogResult.OK)
			{
			 	txtTemplateDir.Text = fldrDialogTemplate.SelectedPath;
			}
		}

		private void RepositoryFrm_Load(object sender, System.EventArgs e)
		{
			//DataEncryptor.RegisterProduct(4);
			string s= DataEncryptor.Instance.GetEncryptedString("asdfghjkl");
			bool b= DataEncryptor.IsAllowed;

			try
			{txtOutputDir.Text= FileUtil.ReadFileData("txtOutputDir.txt").ToString().Trim(); }
			catch{}

			try
			{
				txtTemplateDir.Text= FileUtil.ReadFileData("txtTemplateDir.txt").ToString().Trim(); 
			}
			catch{}
			
			Managers.RepositoryManager.DoYouWantToGenerateEntity +=new DoYouWantToDelegate(RepositoryManager_DoYouWantToGenerateEntity);
			Managers.RepositoryManager.DoYouWantToOverwriteFile +=new DoYouWantToDelegate(RepositoryManager_DoYouWantToOverwriteFile);
			CommonDelegates.OnWarning +=new WarningDelegate(WarnningDelegate_OnWarning);
		}
		
		private bool WarnningDelegate_OnWarning(string message)
		{
			return MessageBox.Show(message,"Warrning",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)== DialogResult.Yes;
		}
		private void btnOutputDir_Click(object sender, System.EventArgs e)
		{
			if(txtOutputDir.Text.Trim() != string.Empty)
			{
				fldrDialogOutput.SelectedPath=txtOutputDir.Text;
			}
			else
			{
				fldrDialogOutput.SelectedPath=Directory.GetCurrentDirectory();
			}

			//open dialog.
			if(fldrDialogOutput.ShowDialog()== DialogResult.OK)
			{
				txtOutputDir.Text = fldrDialogOutput.SelectedPath;
			}
		}

		Hashtable htListViewItems = new Hashtable();
		private void btnLoad_Click(object sender, System.EventArgs e)
		{
              btnGenerate.Visible = false;
                          btnLoad.Visible = false;

            Application.DoEvents();
            try
            {
                FileUtil.CreateTextFile("txtTemplateDir.txt", txtTemplateDir.Text);
                FileUtil.CreateTextFile("txtOutputDir.txt", txtOutputDir.Text);
                DirectoryInfo dir = new DirectoryInfo(txtTemplateDir.Text);
                Managers.RepositoryManager objRepositoryManager = Managers.RepositoryManager.CreateNewInstance(txtTemplateDir.Text, txtOutputDir.Text);


                //for debugging, generating code.
                if (new FileInfo(Configurations.Provider.GeneratingClassFile).Directory.Exists)
                {
                    FileUtil.CreateTextFile(Configurations.Provider.GeneratingClassFile,
                        Managers.GeneratingCodeManager.Instance.CodeGeneratingClass.ToString());
                }

                //get already checked entities
                Hashtable htCheckedEntityNames = new Hashtable();
                foreach (DictionaryEntry entry in htListViewItems)
                {
                    if (((ListViewItem)entry.Value).Checked)
                    {
                        htCheckedEntityNames.Add(entry.Key, entry.Key);
                    }
                }

                //clear lists
                htListViewItems.Clear();
                lvEntities.Items.Clear();

                //repopulate listview and hashtable with items.

                foreach (string key in Managers.RepositoryManager.Instance.Entities.Keys)
                {

                    Definitions.Entity entity = Managers.RepositoryManager.Instance.Entities[key];
                    ListViewItem lvItem = new ListViewItem(new string[] { entity.Name, entity.TableName, entity.ViewName });


                    //mark checked it was previosly checked
                    if (htCheckedEntityNames.ContainsKey(entity.Name))
                    {
                        lvItem.Checked = true;
                    }

                    //now simply add
                    if (!htListViewItems.Contains(entity.Name))
                    {
                        lvItem.ForeColor = Color.OliveDrab;
                        lvEntities.Items.Add(lvItem);
                        htListViewItems.Add(entity.Name, lvItem);
                    }

                }
                return;



                //code for testing
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(string.Format("{0}\\Repository.xml", dir.FullName));


                xmlDoc = new XmlDocument();
                xmlDoc.Load(string.Format("{0}\\EntityDefTesting.xml", dir.FullName));
                Definitions.EntityCollection entityCol = new CodeGenerator.Definitions.EntityCollection(xmlDoc, xmlDoc);


                Definitions.Repository rep = new CodeGenerator.Definitions.Repository(xmlDoc);
                foreach (Definitions.Repository.GeneratedFiles.File file in rep.GeneratedFilesList)
                {
                    Console.Write(file.Name);
                }
                foreach (Definitions.Repository.SessionFiles.File file in rep.SessionFilesList)
                {
                    Console.WriteLine(file.Name);
                }
                Console.WriteLine(dir.GetFiles("ali/a.xml")[0].FullName);
                EXtremecode.Utils.FileUtil.CreateFile("a.xml", dir);

            }
            finally
            {
                btnLoad.Visible = true;
                btnGenerate.Visible = true;
            }
		}

		private void btnGenerate_Click(object sender, System.EventArgs e)
		{	
			//reset cancelled flag.

            btnLoad.Visible = false;
            btnGenerate.Visible = false; 
            Application.DoEvents();
            try
            {
                overwritingCancelled = false;
                Managers.RepositoryManager.Instance.GenerateCodeFiles();
                if (!overwritingCancelled)
                {
                    MessageBox.Show("Succeeded!");
                }
            }
            finally
            {
                btnLoad.Visible = true;
                btnGenerate.Visible = true;

            }
		}

		private bool RepositoryManager_DoYouWantToGenerateEntity(object condition)
		{
			
			string entityName = condition.ToString();
			bool b = true;
			ListViewItem lvItem = htListViewItems[entityName] as ListViewItem;
			if(lvItem != null)
			{
				b=lvItem.Checked;
			}
			return b;
		}

		private bool overwritingCancelled=false;
		private bool RepositoryManager_DoYouWantToOverwriteFile(object condition)
		{
			if(chkOverwrite.Checked)
			{
				return true;
			}
			else if (overwritingCancelled) 
			{
				return false;
			}
			else
			{
				bool overwrite = false;
				DialogResult dialogResult = MessageBox.Show(condition.ToString(),"Want to proceed",
					MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning);
				if( dialogResult == DialogResult.Yes)
				{
					overwrite = true;
				}
				else 
				{
					overwrite = false;

					if(dialogResult == DialogResult.Cancel)
					{
						overwritingCancelled = true; 
					}
				}
				return overwrite;

			}

		}

		private void btnUnselect_Click(object sender, System.EventArgs e)
		{
			foreach(ListViewItem item in lvEntities.Items)
			{
				item.Checked=false;
			}
		}

		private void btnSelectAll_Click(object sender, System.EventArgs e)
		{
			foreach(ListViewItem item in lvEntities.Items)
			{
				item.Checked=true;
			}
		}

		
	}
}
